package com.dai.wechat.controller;


import com.dai.wechat.pojo.ReplyVO;
import com.dai.wechat.util.ParseXmlUtils;
import com.dai.wechat.util.Pusher;
import com.dai.wechat.util.ReplyModel;
import com.dai.wechat.util.Result;
import org.dom4j.DocumentException;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Map;

@RestController
public class PushController {


    private final String openId = "oiX7p50YftQIL1UqP4fqNLuuRInE";


    @RequestMapping("/push")
    public String doPush(){
        Result<String> push = Pusher.push(openId,Pusher.DRINK_TEMPLATEID);
        Pusher.push(openId,Pusher.CONSTELLATION_TEMPLATEID);
        Pusher.push(openId,Pusher.GOODMORNING_TEMPLATEID);

        if("200".equals(push.getCode())){
            return "发送成功！";
        }else{
            return push.getMessage();
        }

    }



    // 消息接收请求方式为 POST
    @PostMapping(value = "/weixin")
    public String doReply(HttpServletRequest request){

        try {
            request.setCharacterEncoding("UTF-8");
            // 获得用户发送的消息
            Map<String, String> stringStringMap = ParseXmlUtils.parseXml(request);
            // 获得响应消息
            String s = ReplyModel.responseReply(stringStringMap);
            return s;

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
     return null;

    }
}
